//
//  SegmentView.m
//  quxiangtao
//
//  Created by E人一铺 on 2018/11/15.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "SegmentView.h"

#import <TYTabPagerView.h>
#import <TYTabPagerBar.h>
#import <TYTabPagerController.h>
#import "BottomGoodsCollectionViewCell.h"
#import "TbCategoryTitleModel.h"
#import "TbGoodsListModel.h"
#import "SegmenGoodsView.h"
#import "JDShoppingListModel.h"
#define kWidth self.frame.size.width
#define kHeight self.frame.size.height

static NSString *bottomGoodsCellID = @"bottomGoodsCellID";

@interface SegmentView() <UICollectionViewDelegate, UICollectionViewDataSource, TYTabPagerViewDelegate, TYTabPagerViewDataSource>


@property (nonatomic, strong) TYTabPagerView *segmentPagerVC;
@property (nonatomic, strong) SegmenGoodsView *v;
/**
 分类数据
 */
@property (nonatomic,strong) NSArray *classArr;

/**
 分类数据
 */
@property (nonatomic,strong) NSMutableArray *goodsViewArr;

@property (copy, nonatomic) NSString *classID;

/**
 记录当前Index
 */
@property (assign, nonatomic) NSInteger oldIndex;

@property (assign, nonatomic) NSInteger newIndex;

@end

@implementation SegmentView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        searchButton.backgroundColor = WhiteColor;
        [searchButton setImage:[UIImage imageNamed:@"搜索拷贝"] forState:UIControlStateNormal];
        [self addSubview:searchButton];
        [searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.mas_equalTo(0);
            make.height.mas_equalTo(jisuan(100));
        }];
         [searchButton addTarget:self action:@selector(preventFlicker:) forControlEvents:UIControlEventAllTouchEvents];
        [searchButton addTarget:self action:@selector(searchClick) forControlEvents:UIControlEventTouchUpInside];

    }
    return self;
}

- (void)preventFlicker:(UIButton *)button {
    button.highlighted = NO;
}

- (void)setViewIsSelfSupportwithTag:(NSInteger)tagValue {
    
    if (tagValue != 0) {
        self.supportType = tagValue;
    }
}

- (void)setSupportType:(NSInteger)supportType {
    
    _supportType = supportType;
}

- (void)setSelectIndex:(NSInteger)selectIndex {
    _selectIndex = selectIndex;
    [self loadData];
    
}


- (void)setParentVC:(UIViewController *)parentVC {
    _parentVC = parentVC;
}

- (void)loadData {
    
    [self getTbCategoryTitleData:self.selectIndex];
    
}

- (void)searchClick {
    
    if (self.updataBlock) {
        self.updataBlock();
    }
}

#pragma mark -获取淘宝下分类标题
- (void)getTbCategoryTitleData:(NSInteger)index {
    
    WS(weakSelf);
    if (index==4) {
        [QNetWorkRequest jdongCommodityClassificationSuccess:^(id response) {
            weakSelf.classArr = [TbCategoryTitleModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            TbCategoryTitleModel *model = weakSelf.classArr[0];
            weakSelf.classID = model.classid;
            weakSelf.oldIndex = 0;
            [weakSelf.segmentPagerVC updateData];
            [weakSelf setupSubViews];
            [weakSelf.v changeModel:model selectButtonIndex:weakSelf.selectIndex];
        }];
    } else {
        NSString *url = @"goods/index/goodsClass";
        
        [TbCategoryTitleModel getCategoryTitleWithUrl:url successBlock:^(id responseObject) {
            if ([responseObject[@"code"] integerValue] == 1) {
                
                NSArray *arr = responseObject[@"data"];
                TbCategoryTitleModel *model = arr[0];
                weakSelf.classID = model.classid;
                weakSelf.oldIndex = 0;
                weakSelf.classArr = arr;
                [weakSelf.segmentPagerVC updateData];
                [weakSelf setupSubViews];
               [weakSelf.v changeModel:model selectButtonIndex:weakSelf.selectIndex];
            }else {
                [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
            }
        } failBlock:^(NSError *error) {
            if (error) {
//                [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
            }
        }];
    }
}

- (void)setupSubViews {
    
    [self addSubview:self.segmentPagerVC];
    [self.segmentPagerVC registerClass:[SegmenGoodsView class] forPagerCellWithReuseIdentifier:@"SegmenGoodsViewID"];
    [self.segmentPagerVC scrollToViewAtIndex:0 animate:YES];
    
    WS(weakSelf)
    [self.segmentPagerVC mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(jisuan(100));
        make.left.mas_equalTo(weakSelf.mas_left).with.offset(0);
        make.right.mas_equalTo(weakSelf.mas_right).with.offset(0);
        make.bottom.mas_equalTo(weakSelf.mas_bottom);
    }];
    
}

#pragma mark - Private Methods
- (void)acceptMsg:(NSNotification *)notification {
    NSString *notificationName = notification.name;
    if ([notificationName isEqualToString:@"goTop"]) {

    } else if ([notificationName isEqualToString:@"leaveTop"]){
        
    }
}

- (NSArray *)classArr {
    
    if (!_classArr) {
        _classArr = [NSArray array];
    }
    return _classArr;
}
- (NSMutableArray *)goodsViewArr {
    
    if (!_goodsViewArr) {
        _goodsViewArr = [NSMutableArray array];
    }
    return _goodsViewArr;
}


- (TYTabPagerView *)segmentPagerVC {
    if (!_segmentPagerVC) {
        _segmentPagerVC = [[TYTabPagerView alloc] init];
        _segmentPagerVC.tabBar.backgroundColor = [UIColor purpleColor];
        _segmentPagerVC.tabBar.layout.progressColor = [UIColor redColor];
        _segmentPagerVC.tabBar.layout.normalTextColor = Color333333;
        _segmentPagerVC.tabBar.layout.selectedTextColor = Colorff0000;
        _segmentPagerVC.tabBar.layout.barStyle = TYPagerBarStyleProgressView;
        _segmentPagerVC.tabBar.layout.progressWidth = jisuan(100);
        _segmentPagerVC.tabBar.backgroundColor = [UIColor whiteColor];
        _segmentPagerVC.tabBar.layout.cellSpacing = 0;
        _segmentPagerVC.tabBar.layout.cellEdging = 0;
        _segmentPagerVC.tabBar.layout.cellWidth =  SCREEN_WIDTH / 5;
        _segmentPagerVC.tabBar.layout.normalTextFont = [UIFont systemFontOfSize:jisuan(24)];
        _segmentPagerVC.tabBar.layout.selectedTextFont = [UIFont systemFontOfSize:jisuan(24)];
        _segmentPagerVC.tabBarHeight = jisuan(90);
        _segmentPagerVC.delegate = self;
        _segmentPagerVC.dataSource = self;
    }
    return _segmentPagerVC;
}


#pragma mark -Page delegate、datasource
- (NSInteger)numberOfViewsInTabPagerView {
    return self.classArr.count;
}

- (NSString *)tabPagerView:(TYTabPagerView *)tabPagerView titleForIndex:(NSInteger)index {
    TbCategoryTitleModel *model = self.classArr[index];
    return model.classname;
}

- (UIView *)tabPagerView:(TYTabPagerView *)tabPagerView viewForIndex:(NSInteger)index prefetching:(BOOL)prefetching {
    
    self.v = (SegmenGoodsView *)[tabPagerView dequeueReusablePagerCellWithReuseIdentifier:@"SegmenGoodsViewID" forIndex:index];
    self.v.parentVC = self.parentVC;
    [self.v changeModel:self.classArr[index] selectButtonIndex:self.selectIndex];
    self.v.supportType = self.supportType;
    return self.v;


}

#pragma mark - UIScrollViewDelegate
//增加分页视图左右滑动和外界tableView上下滑动互斥处理
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [[NSNotificationCenter defaultCenter] postNotificationName:IsEnablePersonalCenterVCMainTableViewScroll object:nil userInfo:@{@"canScroll" : @"0"}];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [[NSNotificationCenter defaultCenter] postNotificationName:IsEnablePersonalCenterVCMainTableViewScroll object:nil userInfo:@{@"canScroll" : @"1"}];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollViewDidEndScrollingAnimation:scrollView];
    
}

@end
